package org.alex.farouq.exam.view;

import java.awt.Color;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;

import org.alex.farouq.exam.controller.Controller;
import org.alex.farouq.exam.model.question.SubQuestion;

public class SubQuestionWindow extends javax.swing.JFrame {

	public Controller control;

	/** Creates new form ScreenN2 */
	public SubQuestionWindow() {
		control = Controller.getInstance();
		control.addObserver(this);
		setLocation(750, 150);
		setResizable(false);
		setTitle("Sub Question");
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jDesktopPane1 = new javax.swing.JDesktopPane();
		subQuestionLabel = new javax.swing.JLabel();
		durationLabel = new javax.swing.JLabel();
		headerLabel = new javax.swing.JLabel();
		typeLabel = new javax.swing.JLabel();
		markLabel = new javax.swing.JLabel();
		durationText = new javax.swing.JTextField();
		headerText = new javax.swing.JTextField();
		markText = new javax.swing.JTextField();
		typeText = new javax.swing.JComboBox();
		jSeparator1 = new javax.swing.JSeparator();
		jSeparator2 = new javax.swing.JSeparator();
		jSeparator3 = new javax.swing.JSeparator();
		jSeparator4 = new javax.swing.JSeparator();
		textLabel = new javax.swing.JLabel();
		jLabel1 = new javax.swing.JLabel();
		textField = new javax.swing.JTextField();
		AnswerType = new javax.swing.JComboBox();
		jSeparator5 = new javax.swing.JSeparator();
		jSeparator6 = new javax.swing.JSeparator();
		jSeparator7 = new javax.swing.JSeparator();
		jSeparator8 = new javax.swing.JSeparator();
		jScrollPane1 = new javax.swing.JScrollPane();
		deleteButton = new javax.swing.JButton();
		editButton = new javax.swing.JButton();
		doneButton = new javax.swing.JButton();
		cancelButton = new javax.swing.JButton();
		addButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		DefaultListModel listModel = new DefaultListModel();
		answersList = new JList(listModel);

		jDesktopPane1.setBackground(new java.awt.Color(255, 255, 255));

		subQuestionLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
		subQuestionLabel.setText("Sub Question :");
		subQuestionLabel.setBounds(20, 10, 100, 17);
		jDesktopPane1.add(subQuestionLabel,
				javax.swing.JLayeredPane.DEFAULT_LAYER);

		durationLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
		durationLabel.setText("Duration :");
		durationLabel.setBounds(20, 170, 60, 15);
		jDesktopPane1
				.add(durationLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

		headerLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
		headerLabel.setText("Header :");
		headerLabel.setBounds(20, 50, 60, 15);
		jDesktopPane1.add(headerLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

		typeLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
		typeLabel.setText("Type :");
		typeLabel.setBounds(20, 90, 60, 15);
		jDesktopPane1.add(typeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

		markLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
		markLabel.setText("Mark :");
		markLabel.setBounds(20, 130, 60, 15);
		jDesktopPane1.add(markLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
		durationText.setText("");

		durationText.setBounds(120, 160, 160, 30);
		jDesktopPane1.add(durationText, javax.swing.JLayeredPane.DEFAULT_LAYER);
		headerText.setText("");

		headerText.setBounds(120, 40, 160, 30);
		jDesktopPane1.add(headerText, javax.swing.JLayeredPane.DEFAULT_LAYER);
		markText.setText("");

		markText.setBounds(120, 120, 160, 30);
		jDesktopPane1.add(markText, javax.swing.JLayeredPane.DEFAULT_LAYER);

		typeText.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"Type 1", "Type 2", "Type 3" }));

		typeText.setBounds(120, 80, 160, 30);
		jDesktopPane1.add(typeText, javax.swing.JLayeredPane.DEFAULT_LAYER);
		jSeparator1.setBounds(20, 340, 360, 10);
		jDesktopPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);
		jSeparator2.setBounds(20, 210, 360, 10);
		jDesktopPane1.add(jSeparator2, javax.swing.JLayeredPane.DEFAULT_LAYER);

		jSeparator3.setOrientation(javax.swing.SwingConstants.VERTICAL);
		jSeparator3.setBounds(380, 210, 10, 130);
		jDesktopPane1.add(jSeparator3, javax.swing.JLayeredPane.DEFAULT_LAYER);

		jSeparator4.setOrientation(javax.swing.SwingConstants.VERTICAL);
		jSeparator4.setBounds(20, 210, 10, 130);
		jDesktopPane1.add(jSeparator4, javax.swing.JLayeredPane.DEFAULT_LAYER);

		textLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
		textLabel.setText("Text :");
		textLabel.setBounds(40, 230, 34, 15);
		jDesktopPane1.add(textLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

		jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
		jLabel1.setText("Type :");
		jLabel1.setBounds(40, 290, 40, 15);
		jDesktopPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

		textField.setBounds(120, 230, 180, 50);
		jDesktopPane1.add(textField, javax.swing.JLayeredPane.DEFAULT_LAYER);

		AnswerType.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"Correct Answer", "Wrong Answer" }));

		AnswerType.setBounds(120, 290, 180, 30);
		jDesktopPane1.add(AnswerType, javax.swing.JLayeredPane.DEFAULT_LAYER);
		jSeparator5.setBounds(20, 480, 360, 10);
		jDesktopPane1.add(jSeparator5, javax.swing.JLayeredPane.DEFAULT_LAYER);
		jSeparator6.setBounds(20, 350, 360, 10);
		jDesktopPane1.add(jSeparator6, javax.swing.JLayeredPane.DEFAULT_LAYER);

		jSeparator7.setOrientation(javax.swing.SwingConstants.VERTICAL);
		jSeparator7.setBounds(380, 350, 10, 130);
		jDesktopPane1.add(jSeparator7, javax.swing.JLayeredPane.DEFAULT_LAYER);

		jSeparator8.setOrientation(javax.swing.SwingConstants.VERTICAL);
		jSeparator8.setBounds(20, 350, 10, 130);
		jDesktopPane1.add(jSeparator8, javax.swing.JLayeredPane.DEFAULT_LAYER);

		jScrollPane1.setViewportView(answersList);

		jScrollPane1.setBounds(50, 360, 300, 80);
		jDesktopPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

		deleteButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
		deleteButton.setText("Delete");
		deleteButton.setName("23");
		deleteButton.addActionListener(control);
		deleteButton.setBounds(219, 450, 73, 23);
		jDesktopPane1.add(deleteButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

		editButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
		editButton.setText("Edit");
		editButton.setName("22");
		editButton.addActionListener(control);
		editButton.setBounds(100, 450, 73, 23);
		jDesktopPane1.add(editButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

		doneButton.setText("Done");
		doneButton.setName("24");
		doneButton.addActionListener(control);
		doneButton.setBounds(70, 490, 70, 23);
		jDesktopPane1.add(doneButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

		cancelButton.setText("Cancel");
		cancelButton.setName("25");
		cancelButton.addActionListener(control);
		cancelButton.setBounds(245, 490, 70, 23);
		jDesktopPane1.add(cancelButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

		addButton.setText("Add");
		addButton.setName("21");
		addButton.addActionListener(control);
		addButton.setBounds(310, 250, 60, 40);
		jDesktopPane1.add(addButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 525,
				javax.swing.GroupLayout.PREFERRED_SIZE));

		pack();
	}// </editor-fold>

	public void editChoiceButtonActionPerformed() {
		// TODO add your handling code here:
	}

	public int deleteChoiceButtonActionPerformed() {
		// you should return array of object to handle exception appear
		int index = answersList.getSelectedIndex();
		if (index != -1) {
			((DefaultListModel) answersList.getModel()).remove(index);
			answersList.updateUI();
		} else {
			JOptionPane.showMessageDialog(this,
					"please select an item to delete");
		}
		return index;
	}

	public void cancelButtonActionPerformed() {
		setVisible(false);
	}

	private boolean validFields() {
		return !headerText.getText().equals("")
				&& !markText.getText().equals("")
				&& !durationText.getText().equals("")
				&& answersList.getModel().getSize() >= 4;
	}

	private boolean validData() {
		try {
			Integer.parseInt(markText.getText());
			Integer.parseInt(durationText.getText());
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}

	public boolean doneButtonActionPerformed() {
		if (!validFields()) {
			JOptionPane.showMessageDialog(this,
					"please insert data in all fields");
			return false;
		} else if (!validData()) {
			JOptionPane.showMessageDialog(this,
					"please insert numbers only in mark and duration feilds");
			return false;
		}

		setVisible(false);
		return true;
	}

	public boolean addChoiceButtonActionPerformed() {
		// TODO add suitable differentiation
		String choiceText = textField.getText();
		if (choiceText.equalsIgnoreCase("")) {
			return false;
		}

		if (AnswerType.getSelectedItem().equals("Correct Answer")) {
			answersList.setForeground(Color.green);
		} else {
			answersList.setForeground(Color.red);
		}

		((DefaultListModel) answersList.getModel()).addElement(choiceText);
		answersList.updateUI();
		return true;
	}

	public void showEditedSubQuestion(String header, int typeIndex,
			String mark, String duration, String[] answers) {
		headerText.setText(header);
		typeText.setSelectedIndex(typeIndex);
		markText.setText(mark);
		durationText.setText(duration);
		textField.setText("");
		AnswerType.setSelectedIndex(0);

		((DefaultListModel) answersList.getModel()).removeAllElements();
		for (String answer : answers) {
			((DefaultListModel) answersList.getModel()).addElement(answer);
		}
		answersList.updateUI();
	}

	public boolean isCorrectChoice() {
		// TODO Check wether the selected choice is correct or wrong
		return false;
	}

	public javax.swing.JTextField getHeaderText() {
		return headerText;
	}

	// Variables declaration - do not modify
	private javax.swing.JComboBox AnswerType;
	private javax.swing.JButton addButton;
	private javax.swing.JList answersList;
	private javax.swing.JButton cancelButton;
	private javax.swing.JButton deleteButton;
	private javax.swing.JButton doneButton;
	private javax.swing.JLabel durationLabel;
	private javax.swing.JTextField durationText;
	private javax.swing.JButton editButton;
	private javax.swing.JLabel headerLabel;
	private javax.swing.JTextField headerText;
	private javax.swing.JDesktopPane jDesktopPane1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JSeparator jSeparator2;
	private javax.swing.JSeparator jSeparator3;
	private javax.swing.JSeparator jSeparator4;
	private javax.swing.JSeparator jSeparator5;
	private javax.swing.JSeparator jSeparator6;
	private javax.swing.JSeparator jSeparator7;
	private javax.swing.JSeparator jSeparator8;
	private javax.swing.JLabel markLabel;
	private javax.swing.JTextField markText;
	private javax.swing.JLabel subQuestionLabel;
	private javax.swing.JTextField textField;
	private javax.swing.JLabel textLabel;
	private javax.swing.JLabel typeLabel;
	private javax.swing.JComboBox typeText;
	// End of variables declaration

}
